﻿using SGA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;

namespace SGA.Classes
{
    class Generic
    {
        private const int K_ID_CREDITORE = 100668;

        private static Generic instance = new Generic();
        private Generic() { }

        /// <summary>
        /// Singleton Generic
        /// </summary>
        public static Generic Instance
        {
            get
            {
                return instance;
            }
        }


        /// <summary>
        /// Obtém o id do utilizador autenticado
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public int getUserId(String UserName)
        {
            //string login = Page.User.Identity.Name;
            int userId;

            using (var bd = new MailingEntities())
            {
                var usr = (from user in bd.SystemUsers
                           where user.Username == UserName
                           select user).SingleOrDefault();
                userId = usr.Id;
            }
            return userId;
        }

        /// <summary>
        /// obtém o Aliás do utilizador autenticado
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string getUserAlias(int userId)
        {
            string userAlias;

            using (var bd = new MailingEntities())
            {
                var usr = (from user in bd.SystemUsers
                           where user.Id == userId
                           select user).SingleOrDefault();
                userAlias = usr.Alias;
            }
            return userAlias;
        }


        public string getUserAlias(String UserName)
        {
            string userAlias;

            using (var bd = new MailingEntities())
            {
                var usr = (from user in bd.SystemUsers
                           where user.Username == UserName
                           select user).SingleOrDefault();
                userAlias = usr.Alias;
            }
            return userAlias;
        }


        public string formatDateToString(DateTime? date)
        {
            DateTime dt;
            dt = Convert.ToDateTime(date);
            return string.Format("{0:dd-MM-yyyy}", dt);
        }

        /// <summary>
        /// Obtém o número sequencial da autorização bancária
        /// </summary>
        /// <param name="IdSupporter"></param>
        /// <param name="sequence"></param>
        /// <returns></returns>
        public string getResumenSequence(int IdSupporter, string sequence)
        {
            using (MailingEntities bd = new MailingEntities())
            {
                string qry = bd.info_num_aut(K_ID_CREDITORE.ToString(), IdSupporter, sequence).SingleOrDefault().ToString();
                return  Regex.Replace(qry, @"(\w{9})(\w{2})", @"$1 $2");
            }
        }


        /// <summary>
        /// Verifica se a string tem apenas caracteres numéricos
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool isNumeric(string value)
        {
            //Regex regex = new Regex(@"^\d$");
            Regex regex = new Regex("^[0-9]+$");
            return regex.IsMatch(value);
        }


        public int Modulo(string sModulus, int iTeiler)
        {
            int iStart, iEnde, iErgebniss, iRestTmp, iBuffer;
            string iRest = "", sErg = "";

            iStart = 0;
            iEnde = 0;

            while (iEnde <= sModulus.Length - 1)
            {
                iBuffer = int.Parse(iRest + sModulus.Substring(iStart, iEnde - iStart + 1));

                if (iBuffer >= iTeiler)
                {
                    iErgebniss = iBuffer / iTeiler;
                    iRestTmp = iBuffer - iErgebniss * iTeiler;
                    iRest = iRestTmp.ToString();

                    sErg = sErg + iErgebniss.ToString();

                    iStart = iEnde + 1;
                    iEnde = iStart;
                }
                else
                {
                    if (sErg != "")
                        sErg = sErg + "0";

                    iEnde = iEnde + 1;
                }
            }

            if (iStart <= sModulus.Length)
                iRest = iRest + sModulus.Substring(iStart);

            return int.Parse(iRest);
        }



    }
}